package com.nut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nut.common.Result;
import com.nut.entity.SysDict;
import com.nut.mapper.SysDictMapper;
import com.nut.service.SysDictService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Set;

@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Override
    public Result<IPage<SysDict>> list(String name, Integer pageNo, Integer pageSize) {
        Page<SysDict> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(name)) {
            queryWrapper.eq(SysDict::getName, name);
        }
        queryWrapper.orderByDesc(SysDict::getId);

        IPage<SysDict> list = baseMapper.selectPage(page, queryWrapper);
        return Result.ok(list);
    }

    @Override
    public List<SysDict> getByPid(Integer pid) {
        LambdaQueryWrapper<SysDict> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDict::getPid, pid);
        queryWrapper.orderByAsc(SysDict::getSort);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        baseMapper.deleteBatchIds(ids);
        return Result.ok();
    }

}
